import { createRouter, createWebHistory } from "vue-router";
import VueCookies from "vue-cookies";
import getPageTitle from "@/utils/GetPageTitle";
const routes = [
  {
    name: '登陆',
    path: '/login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登陆'
    }
  },
  {
    name: '框架页面',
    path: '/',
    component: () => import('../views/Framework.vue'),
    redirect: '/blog/list',
    children: [
      {
        path: '/blog/list',
        name: '博客列表',
        component: () => import('../views/blog/BlogList.vue'),
        meta: {
          title: '博客列表'
        }
      },
      {
        path: '/blog/category',
        name: '分类管理',
        component: () => import('../views/blog/BlogCategory.vue'),
        meta: {
          title: '分类管理'
        }
      },
      { 
        path: '/special/list', 
        name: '专题管理',
        component: () => import('../views/special/SpecialList.vue'), 
        meta: {
          title: '专题管理'
        }
      },
      { 
        path: '/setting/myInfo', 
        name: '个人信息',
        component: () => import('../views/setting/MyInfo.vue'),
        meta: {
          title: '个人信息'
        }
      },
      { 
        path: '/setting/users', 
        name: '博客成员',
        component: () => import('../views/setting/TeamUser.vue'),
        meta: {
          title: '博客成员'
        }
      },
      { 
        path: '/setting/systemSetting', 
        name: '系统设置',
        component: () => import('../views/setting/System.vue'),
        meta: {
          title: '系统设置'
        }
      },
      { 
        path: '/recovery/list', 
        name: '回收站',
        component: () => import('../views/recovery/RecoveryList.vue'),
        meta: {
          title: '回收站'
        }
      },
    ]

  }
]

const router = createRouter({
  routes,
  history: createWebHistory()
})

// 路由守卫
router.beforeEach((to, _, next) => {
  const userInfo = VueCookies.get("userInfo")
  if (!userInfo && to.path != "/login") {
    router.push("/login")
  }
  document.title = getPageTitle(to.meta.title);
  next()
})

export default router